package com.tengju.bff.interfaces.manage.crm.brand.assembler;

import com.tengju.bff.interfaces.shared.*;
import com.tengju.bff.interfaces.shared.Formatter;
import com.tengju.data.domain.model.analysis.Row;
import com.tengju.data.domain.model.clickhouse.CrmRankCrowdPerspective;
import com.tengju.data.domain.shared.CastUtil;
import com.tengju.user.domain.model.doc.UserDocument;
import com.tengju.user.domain.model.user.StarLevelEnum;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


public class BrandAnalysisRankAssembler {

    public static TableVO convertToVo(List<Row> rows, TableVO tableVO, List<UserDocument> userDocuments, List<UserDocument> leaderUserDocuments, CrmRankCrowdPerspective crmRankCrowdPerspective) {


        String showLabel = crmRankCrowdPerspective.getShowLabel();
        tableVO.addColumn(null, ColumnVO.of("showName",showLabel,false));
        if (CrmRankCrowdPerspective.CITY_STAR == crmRankCrowdPerspective) {
            tableVO.addColumn("dealer_turnover_active", ColumnVO.of("belongEc","所属EC",false));
        } else if (CrmRankCrowdPerspective.DEALER == crmRankCrowdPerspective) {
            tableVO.addColumn("dealer_turnover_active", ColumnVO.of("belongCity","所属城市服务商",false));
            tableVO.addColumn("belongCity", ColumnVO.of("belongEc","所属EC",false));
        }

        Map<Long, UserDocument> userDocumentMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(userDocuments)) {
            userDocumentMap = userDocuments.stream()
                    .collect(Collectors.toMap(UserDocument::getIdCode, Function.identity()));
        }

        Map<Long, UserDocument> leaderUserDocumentMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(leaderUserDocuments)) {
            leaderUserDocumentMap = leaderUserDocuments.stream()
                    .collect(Collectors.toMap(UserDocument::getIdCode, Function.identity()));
        }


        //封装默认的表格数据
        if (CollectionUtils.isEmpty(rows)) {
            return tableVO;
        }
        List<Map<String, Object>> records = new ArrayList<>();
        for (Row row : rows) {
            Map<String, Object> record = new HashMap<>();
            //销售额
            Object saleMoney = row.getValue("sale_money");
            Integer rank = Optional.ofNullable(row.getValue("rank")).map(CastUtil::castInt).orElse(null);
            if (saleMoney == null || rank == null || rank == 0) {
                continue;
            }
            record.put("sale_money",Formatter.thousandMoney(BigDecimal.valueOf((double) saleMoney).setScale(0,RoundingMode.HALF_UP).intValue()));
            //idCode
            Long idCode = CastUtil.castLong(row.getValue("id_code"));
            record.put("id_code",idCode);
            //会员成交人数
            Object dealerTurnover = row.getValue("dealer_turnover");
            record.put("dealer_turnover",dealerTurnover);
            //会员成交活跃度
            String dealerTurnoverActiveStr = "0%";
            Object dealerTurnoverActive = row.getValue("dealer_turnover_active");
            if (dealerTurnoverActive != null) {
                dealerTurnoverActiveStr = BigDecimal.valueOf((Double) row.getValue("dealer_turnover_active") * 100).setScale(2, RoundingMode.FLOOR).toString() + '%';
            }

            record.put("dealer_turnover_active",dealerTurnoverActiveStr);
            //排名
            Integer lastRank = Optional.ofNullable(row.getValue("lastRank")).map(CastUtil::castInt).orElse(null);
            record.put("rankVO",RankVO.of(rank,lastRank));
            UserDocument userDocument = userDocumentMap.get(idCode);
            record.put("showName",genShowName(userDocument));
            if (CrmRankCrowdPerspective.CITY_STAR == crmRankCrowdPerspective) {
                UserDocument belongEc = leaderUserDocumentMap
                        .get(Optional.ofNullable(userDocument).map(UserDocument::getOwnerEcIdCode).orElse(0L));
                record.put("belongEc",genShowName(belongEc));
            } else if (CrmRankCrowdPerspective.DEALER == crmRankCrowdPerspective) {
                if (Optional.ofNullable(userDocument.getStarLevel()).orElse(0) < StarLevelEnum.EC.getCode()) {
                    UserDocument belongEc = leaderUserDocumentMap
                            .get(Optional.of(userDocument).map(UserDocument::getOwnerEcIdCode).orElse(0L));
                    record.put("belongEc",genShowName(belongEc));
                }
                if (Optional.ofNullable(userDocument.getStarLevel()).orElse(0) < StarLevelEnum.CITY_STAR_ONE.getCode()) {
                    UserDocument belongCity = leaderUserDocumentMap
                            .get(Optional.of(userDocument).map(UserDocument::getOwnerCityIdCode).orElse(0L));
                    record.put("belongCity",genShowName(belongCity));
                }
            }
            records.add(record);
        }
        tableVO.setRecords(records);

        return tableVO;
    }

    public static void main(String[] args) {
        System.out.println(new BigDecimal("1212.226123").setScale(0,RoundingMode.HALF_UP).intValue());
    }


    public static String genShowName(UserDocument userDocument) {
        if (userDocument == null) {
            return "";
        }
        return userDocument.getNickName() + " " +
                StarLevelEnum.parseByCode(userDocument.getStarLevel()).getShortName() + " " +
                "(" + userDocument.getIdCode() + ")";
    }
}
